var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buthidden: false,
    ifShowTips: true,
    ifShownoData: true,
    orderStatus: [
      {
        "statusName": "全部订单",
        "statusCode":""
      },
      {
        "statusName": "进行中",
        "statusCode": 1
      },
      {
        "statusName": "已完成",
        "statusCode": 2
      },
      {
        "statusName": "已取消",
        "statusCode": 3
      }
    ],

    statusSelect: "",//判断是否选中
    wOrderVo:"",
    bId:"",
    wxType:"",
    pageIndex: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this ;
    that.setData({ wxType: app.globalData.wxType });
    
    if (options.bId != undefined) {
      that.setData({ bId: options.bId });
    }else {
      that.setData({ bId: ''});
    }
    
    if (app.globalData.userId) { 
      that.setData({ ifShowTips: true });
      that.onOrderInfo();  
    } else {
      that.setData({ ifShowTips: false });
    } 
  },

  goSetting: function () {

    wx.navigateTo({
      url: '../mine/accountSetting',
    })

  },

  orderStatusList: function (e) {
    var that = this;
    that.setData({//把选中值放入判断值
      statusSelect: e.currentTarget.dataset.select,
      pageIndex: 1,
      wOrderVo: " ",
    });
     that.onOrderInfo();
  },

  //进行中订单列表信息
  onOrderInfo: function () {
    var that = this;
    wx.showLoading({
      title: '拼命加载中',
    });
    var arr = new Array;
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/serchReceiptOrderList.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        receiptId: app.globalData.userId,
        status: that.data.statusSelect,
        page: that.data.pageIndex,
        size:8
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.wOrderVo.length > 0) {
          for (var i = 0; i < res.data.wOrderVo.length; i++) {
            let createTime = res.data.wOrderVo[i].createTime.substring(0, 19);
            res.data.wOrderVo[i].createTime = createTime;
          }
          arr = res.data.wOrderVo;
          that.setData({ ifShownoData: true});
        }else{
          that.setData({ ifShownoData: false});
        }

        if (that.data.pageIndex != 1) {
          var array = that.data.wOrderVo;
          array = array.concat(arr);
          that.setData({ ifShownoData: true, wOrderVo: array });
        } else {
          that.setData({ wOrderVo: arr });
        }

      },
      fail: function (error) {
      }
    })

  },
  //去定价事件
  toPriced: function (e) {
    var bId = e.currentTarget.dataset.bid;
    var that = this;
    wx.navigateTo({
      url: '../home/priced?bId=' + bId,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  //致电用户事件
  phonecallevent: function (e) {
    
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel
    })

  },

  //取消订单事件
  ifCancel: function (e) {

    var that = this;
    var bId = e.currentTarget.dataset.bid;

    wx.showModal({
      // title: '提示',
      content: '确定取消该订单吗？',
      cancelText: '再等等',
      confirmColor: '#56bde0',
      success: function (res) {

        if (res.confirm) {
          that.setData({ buthidden: true });
          wx.request({
            method: "POST",
            url: 'https://www.zaishengwang.com/wx/updateOrderStatus.do',
            dataType: 'json',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            data: {
              bId: bId,
              status: 3,
            },
            success: function (res) {
              if (res.data.msg != null) {
                that.onOrderInfo();
                that.setData({ buthidden: false });
              }

            },
            fail: function (error) {
              that.setData({ buthidden: false });
            }

          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      pageIndex: 1, // 每次触发下拉事件pageIndex=1
      wOrderVo: "",
      ifShownoData: true,
    });
    that.onOrderInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      ifShownoData: true,
    })
    // 上拉获取更多数据
    that.onOrderInfo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})